package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.ProcurementBean;
import com.project.bean.ShopBean;
import com.project.bean.UserBean;
import com.project.service.IProcurementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Map;

/**
 * 采购应用控制器
 */
@RestController
@RequestMapping("procurement")
public class ProcurementController {
    @Autowired
    private IProcurementService procurementService;
    /**
     * 入库
     * @param procurement 采购对象
     */
    @RequestMapping("addRespository")
    public String addRespository(ProcurementBean procurement, HttpServletRequest request){
        //从session中取出登陆用户
        UserBean user = (UserBean)request.getSession().getAttribute("loginUser");
        //从session中取出采购车
        Map<String,ShopBean> buyMap = (Map<String,ShopBean>)request.getSession()
                .getAttribute("buyMap");
        //设置采购员
        procurement.setBuyer(user.getRealName());

        //调用业务方法，完成采购入库
        procurementService.addRespository(procurement,buyMap);

        return "ok";
    }

    /**
     * 动态条件分页查询采购信息
     * @param pageNO 页码
     * @param startDate 采购起始日期
     * @param endDate 采购结束日期
     * @return 分页对象
     */
    @RequestMapping("findByItem")
    public IPage<ProcurementBean> findByItem(Integer pageNO, LocalDate startDate, LocalDate endDate){
        return procurementService.findByItem(pageNO,startDate,endDate);
    }


    /**
     * 按采购ID查询采购信息
     * @param id 采购ID
     * @return 采购对象
     */
    @RequestMapping("findById")
    public ProcurementBean findById(Integer id){
        return procurementService.findById(id);
    }
}
